package com.lv.fast.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lv.fast.DO.SysMenuDO;
import com.lv.fast.DTO.SysMenuDTO;
import com.lv.fast.dao.SysMenuDao;
import com.lv.fast.service.SysMenuService;
import com.lv.fast.util.Assert;
import com.lv.fast.util.SysUserUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author jie.lv
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl implements SysMenuService {

    private final SysMenuDao sysMenuDao;

    @Override
    public LinkedHashSet<SysMenuDTO> listByRoleId(String roleId){
        Assert.assertNotNull(roleId, "角色Id不能为空");
        LinkedHashSet<SysMenuDTO> sysMenuDTOS = sysMenuDao.listMenusByRoleId(roleId);
        return sysMenuDTOS;
    }

    @Override
    public LinkedHashSet<SysMenuDTO> listByParentId(String parentId){
        Assert.assertNotNull(parentId, "菜单父Id不能为空");
        QueryWrapper<SysMenuDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.orderByAsc("type", "order_num");
        queryWrapper.orderByDesc("updated_time");
        List<SysMenuDO> sysMenuDOS = sysMenuDao.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(sysMenuDOS)){
            return null;
        }
        LinkedHashSet<SysMenuDTO> menuDTOS = sysMenuDOS.stream().map(sysMenuDO -> BeanUtil.copyProperties(sysMenuDO, SysMenuDTO.class))
                .collect(Collectors.toCollection(LinkedHashSet::new));
        return menuDTOS;
    }

    @Override
    public LinkedHashSet<SysMenuDTO> listAll(){
        QueryWrapper<SysMenuDO> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("type", "order_num");
        queryWrapper.orderByDesc("updated_time");
        List<SysMenuDO> sysMenuDOS = sysMenuDao.selectList(queryWrapper);
        if (sysMenuDOS == null){
            return null;
        }
        LinkedHashSet<SysMenuDTO> sysMenuDTOS = sysMenuDOS.stream().map(sysMenuDO -> BeanUtil.copyProperties(sysMenuDO, SysMenuDTO.class))
                .collect(Collectors.toCollection(LinkedHashSet::new));
        return sysMenuDTOS;
    }

    @Override
    public SysMenuDTO get(String id){
        Assert.assertNotNull(id, "菜单Id不能为空");
        QueryWrapper<SysMenuDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        SysMenuDO sysMenuDO = sysMenuDao.selectOne(queryWrapper);
        SysMenuDTO sysMenuDTO = sysMenuDO.toDTO();
        return sysMenuDTO;
    }

    @Override
    public boolean add(SysMenuDTO menuDTO){
        Assert.assertNotNull(menuDTO, "菜单对象不能为空");
        SysMenuDO sysMenuDO = menuDTO.toDO();
        sysMenuDO.setCreatedBy(SysUserUtil.getCurrentLoginUserId());
        sysMenuDO.setUpdatedBy(SysUserUtil.getCurrentLoginUserId());
        sysMenuDao.insert(sysMenuDO);
        return true;
    }

    @Override
    public boolean update(SysMenuDTO menuDTO){
        Assert.assertNotNull(menuDTO, "菜单对象不能为空");
        SysMenuDO sysMenuDO = BeanUtil.copyProperties(menuDTO, SysMenuDO.class);
        sysMenuDO.setUpdatedBy(SysUserUtil.getCurrentLoginUserId());
        sysMenuDao.updateById(sysMenuDO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean delete(String id){
        //删除菜单角色关联关系
        sysMenuDao.deleteRoleMenuByMenuId(id);
        //删除菜单
        sysMenuDao.deleteById(id);
        return true;
    }
}
